package com.sumainfor.pc.market.service.search;

import com.google.common.collect.Lists;
import com.sumainfor.common.entity.SearchEntity;
import com.sumainfor.common.util.DateUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.HtmlUtil;
import com.sumainfor.pc.market.dao.search.SearchMapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能描述:
 * 市场部搜索
 * @Auther: zhlu
 * @Date: 2019/6/26 18:12
 * @Description:
 */
@Service
public class SearchService {

    @Autowired
    SearchMapper SearchMapper;

    /**
     * 功能描述:
     * 市场部
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/26 19:04
     */
    public JsonResult marketSearch(Integer roleId,String keyWord){
        switch (roleId){
            case 1:
                return searchForSfzy(keyWord);
            case 2:
                return searchForAll(keyWord);
            case 3:
                return searchForZlgw(keyWord);
            case 4:
                return searchForAll(keyWord);
            default:
                return JsonResult.error("无法匹配部门:");
        }
    }

    /**
     * 功能描述:
     * 根据关键字搜索收房专员
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/26 19:05
     */
    public JsonResult searchForSfzy(String search){
        List<SearchEntity> dataList = Lists.newArrayList();

        //搜索字段不为空
        if(!StringUtils.isBlank(search)){
            List<String>searchList= Arrays.asList(search.split("#"));
            if(searchList.size()>0){
                //根据关键字搜索项目资料
                SearchEntity Search1 = new SearchEntity();
                List<Map<String,Object>>getSearchByCommunityList=SearchMapper.getSearchByCommunityList(searchList);
                Search1.setName(String.format("小区信息(%d)", getSearchByCommunityList.size()));
                Search1.setRouter("/sys-sell/housingAverage");
                Search1.setList(getSearchByCommunityList.stream().map(myItem -> {
                    StringBuffer myItemStr = new StringBuffer();
                    DateUtils.formatDateByMap(myItem);
                    myItemStr.append(HtmlUtil.BR)
                            .append("项目编号:").append(MapUtils.getString(myItem, "ComId"))
                            .append(HtmlUtil.BR)
                            .append("项目编码:").append(MapUtils.getString(myItem, "ComCod"))
                            .append(HtmlUtil.BR)
                            .append("项目名称:").append(MapUtils.getString(myItem, "ComName"))
                            .append(HtmlUtil.BR)
                            .append("项目地址:").append(MapUtils.getString(myItem, "Province"))
                            .append(MapUtils.getString(myItem, "City"))
                            .append("-")
                            .append(MapUtils.getString(myItem, "District"))
                            .append("-")
                            .append(MapUtils.getString(myItem, "Street"))
                            .append("-")
                            .append(MapUtils.getString(myItem, "Address"));

                    return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "ComId"), Color.red);

                }).collect(Collectors.toList()));
                dataList.add(Search1);


                //根据关键字搜索房源详表
                SearchEntity Search2 = new SearchEntity();
                List<Map<String,Object>>getSearchByHousingInfoList=SearchMapper.getSearchByHousingInfoList(searchList);
                Search2.setName(String.format("房源信息(%d)", getSearchByHousingInfoList.size()));
                Search2.setRouter("/sys-sell/allhousing");
                Search2.setList(getSearchByHousingInfoList.stream().map(myItem -> {
                    StringBuffer myItemStr = new StringBuffer();
                    String State="";
                    //房源状态（0 待上架 1可签约 2已签约）
                    if(MapUtils.getInteger(myItem, "State").equals(0)){
                        State="待上架";
                    }else if(MapUtils.getInteger(myItem, "State").equals(1)){
                        State="可签约";
                    }else if(MapUtils.getInteger(myItem, "State").equals(2)){
                        State="已签约";
                    }
                    DateUtils.formatDateByMap(myItem);
                    myItemStr.append(HtmlUtil.BR)
                            .append("房源编号:").append(MapUtils.getString(myItem, "HouInfoId"))
                            .append(HtmlUtil.BR)
                            .append("房源名称:").append(MapUtils.getString(myItem, "HouInfoName"))
                            .append(HtmlUtil.BR)
                            .append("房源状态:").append(State);

                    return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "HouInfoId"), Color.red);

                }).collect(Collectors.toList()));
                dataList.add(Search2);

                //根据关键字搜索业主
                SearchEntity Search3 = new SearchEntity();
                List<Map<String,Object>>getSearchByOwnerList=SearchMapper.getSearchByOwnerList(searchList);
                Search3.setName(String.format("业主信息(%d)", getSearchByOwnerList.size()));
                Search3.setRouter("/sys-sell/contact");
                Search3.setList(getSearchByOwnerList.stream().map(myItem -> {
                    StringBuffer myItemStr = new StringBuffer();
                    String Status="";
                    //合同状态   0.履约中   1.已到期   2.未收录
                    if(!FieldCheckUtil.isMapFieldNotNull(myItem,new String[]{"OwDeId"})){
                        Status="未收录";
                    }else if(MapUtils.getInteger(myItem, "Status").equals(0)){
                        Status="履约中";
                    }else if(MapUtils.getInteger(myItem, "Status").equals(1)){
                        Status="已到期";
                    }
                    DateUtils.formatDateByMap(myItem);
                    myItemStr.append(HtmlUtil.BR)
                            .append("业主编号:").append(MapUtils.getString(myItem, "OwId"))
                            .append(HtmlUtil.BR)
                            .append("业主名称:").append(MapUtils.getString(myItem, "OwName"))
                            .append(HtmlUtil.BR)
                            .append("联系方式:").append(MapUtils.getString(myItem, "OwPhone"))
                            .append(HtmlUtil.BR)
                            .append("收录状态:").append(Status);

                    return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "OwId"), Color.red);

                }).collect(Collectors.toList()));
                dataList.add(Search3);
            }

        }
        return JsonResult.success(dataList);
    }


    /**
     * 功能描述:
     * 根据关键字搜索租赁顾问
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/26 19:05
     */
    public JsonResult searchForZlgw(String search){
        List<SearchEntity> dataList = Lists.newArrayList();

        //搜索字段不为空
        if(!StringUtils.isBlank(search)){
            List<String>searchList= Arrays.asList(search.split("#"));
            if(searchList.size()>0){
                //根据关键字搜索房源详表
                SearchEntity Search1 = new SearchEntity();
                List<Map<String,Object>>getSearchByHousingInfoList=SearchMapper.getSearchByHousingInfoList(searchList);
                Search1.setName(String.format("房源信息(%d)", getSearchByHousingInfoList.size()));
                Search1.setRouter("/sys-ServiceManager/houseCon");
                Search1.setList(getSearchByHousingInfoList.stream().map(myItem -> {
                    StringBuffer myItemStr = new StringBuffer();
                    String State="";
                    //房源状态（0 待上架 1可签约 2已签约）
                    if(MapUtils.getInteger(myItem, "State").equals(0)){
                        State="待上架";
                    }else if(MapUtils.getInteger(myItem, "State").equals(1)){
                        State="可签约";
                    }else if(MapUtils.getInteger(myItem, "State").equals(2)){
                        State="已签约";
                    }
                    DateUtils.formatDateByMap(myItem);
                    myItemStr.append(HtmlUtil.BR)
                            .append("房源编号:").append(MapUtils.getString(myItem, "HouInfoId"))
                            .append(HtmlUtil.BR)
                            .append("房源名称:").append(MapUtils.getString(myItem, "HouInfoName"))
                            .append(HtmlUtil.BR)
                            .append("房源状态:").append(State);

                    return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "HouInfoId"), Color.red);

                }).collect(Collectors.toList()));
                dataList.add(Search1);

                //根据关键字搜索租客
                SearchEntity Search2 = new SearchEntity();
                List<Map<String,Object>>getSearchByTenantList=SearchMapper.getSearchByTenantList(searchList);
                Search2.setName(String.format("租客信息(%d)", getSearchByTenantList.size()));
                Search2.setRouter("/sys-housekeeper/tenant");
                Search2.setList(getSearchByTenantList.stream().map(myItem -> {
                    StringBuffer myItemStr = new StringBuffer();
                    DateUtils.formatDateByMap(myItem);
                    myItemStr.append(HtmlUtil.BR)
                            .append("租客编号:").append(MapUtils.getString(myItem, "TenId"))
                            .append(HtmlUtil.BR)
                            .append("租客名称:").append(MapUtils.getString(myItem, "TenName"))
                            .append(HtmlUtil.BR)
                            .append("联系方式:").append(MapUtils.getString(myItem, "TenMode"))
                            .append(HtmlUtil.BR)
                            .append("履约中:").append(MapUtils.getString(myItem, "SigningCount1"))
                            .append(HtmlUtil.BR)
                            .append("已退租:").append(MapUtils.getString(myItem, "SigningCount2"))
                            .append(HtmlUtil.BR)
                            .append("转租成功:").append(MapUtils.getString(myItem, "SigningCount3"))
                            .append(HtmlUtil.BR);

                    return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "TenId"), Color.red);

                }).collect(Collectors.toList()));
                dataList.add(Search2);
            }

        }
        return JsonResult.success(dataList);
    }

    /**
     * 功能描述:
     * 根据关键字搜索市场部经理和服务中心经理
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/26 19:05
     */
    public JsonResult searchForAll(String search){
        List<SearchEntity> dataList = Lists.newArrayList();

        //搜索字段不为空
        if(!StringUtils.isBlank(search)){
            List<String>searchList= Arrays.asList(search.split("#"));
            if(searchList.size()>0){
                //根据关键字搜索项目资料
                SearchEntity Search1 = new SearchEntity();
                List<Map<String,Object>>getSearchByCommunityList=SearchMapper.getSearchByCommunityList(searchList);
                Search1.setName(String.format("小区信息(%d)", getSearchByCommunityList.size()));
                Search1.setRouter("/sys-sell/housingAverage");
                Search1.setList(getSearchByCommunityList.stream().map(myItem -> {
                    StringBuffer myItemStr = new StringBuffer();
                    DateUtils.formatDateByMap(myItem);
                    myItemStr.append(HtmlUtil.BR)
                            .append("项目编号:").append(MapUtils.getString(myItem, "ComId"))
                            .append(HtmlUtil.BR)
                            .append("项目编码:").append(MapUtils.getString(myItem, "ComCod"))
                            .append(HtmlUtil.BR)
                            .append("项目名称:").append(MapUtils.getString(myItem, "ComName"))
                            .append(HtmlUtil.BR)
                            .append("项目地址:").append(MapUtils.getString(myItem, "Province"))
                            .append(MapUtils.getString(myItem, "City"))
                            .append("-")
                            .append(MapUtils.getString(myItem, "District"))
                            .append("-")
                            .append(MapUtils.getString(myItem, "Street"))
                            .append("-")
                            .append(MapUtils.getString(myItem, "Address"));

                    return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "ComId"), Color.red);

                }).collect(Collectors.toList()));
                dataList.add(Search1);


                //根据关键字搜索房源详表
                SearchEntity Search2 = new SearchEntity();
                List<Map<String,Object>>getSearchByHousingInfoList=SearchMapper.getSearchByHousingInfoList(searchList);
                Search2.setName(String.format("房源信息(%d)", getSearchByHousingInfoList.size()));
                Search2.setRouter("/sys-ServiceManager/houseCon");
                Search2.setList(getSearchByHousingInfoList.stream().map(myItem -> {
                    StringBuffer myItemStr = new StringBuffer();
                    String State="";
                    //房源状态（0 待上架 1可签约 2已签约）
                    if(MapUtils.getInteger(myItem, "State").equals(0)){
                        State="待上架";
                    }else if(MapUtils.getInteger(myItem, "State").equals(1)){
                        State="可签约";
                    }else if(MapUtils.getInteger(myItem, "State").equals(2)){
                        State="已签约";
                    }
                    DateUtils.formatDateByMap(myItem);
                    myItemStr.append(HtmlUtil.BR)
                            .append("房源编号:").append(MapUtils.getString(myItem, "HouInfoId"))
                            .append(HtmlUtil.BR)
                            .append("房源名称:").append(MapUtils.getString(myItem, "HouInfoName"))
                            .append(HtmlUtil.BR)
                            .append("房源状态:").append(State);

                    return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "HouInfoId"), Color.red);

                }).collect(Collectors.toList()));
                dataList.add(Search2);

                //根据关键字搜索租客
                SearchEntity Search3 = new SearchEntity();
                List<Map<String,Object>>getSearchByTenantList=SearchMapper.getSearchByTenantList(searchList);
                Search3.setName(String.format("租客信息(%d)", getSearchByTenantList.size()));
                Search3.setRouter("/sys-housekeeper/tenant");
                Search3.setList(getSearchByTenantList.stream().map(myItem -> {
                    StringBuffer myItemStr = new StringBuffer();
                    DateUtils.formatDateByMap(myItem);
                    myItemStr.append(HtmlUtil.BR)
                            .append("租客编号:").append(MapUtils.getString(myItem, "TenId"))
                            .append(HtmlUtil.BR)
                            .append("租客名称:").append(MapUtils.getString(myItem, "TenName"))
                            .append(HtmlUtil.BR)
                            .append("联系方式:").append(MapUtils.getString(myItem, "TenMode"))
                            .append(HtmlUtil.BR)
                            .append("履约中:").append(MapUtils.getString(myItem, "SigningCount1"))
                            .append(HtmlUtil.BR)
                            .append("已退租:").append(MapUtils.getString(myItem, "SigningCount2"))
                            .append(HtmlUtil.BR)
                            .append("转租成功:").append(MapUtils.getString(myItem, "SigningCount3"))
                            .append(HtmlUtil.BR);

                    return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "TenId"), Color.red);

                }).collect(Collectors.toList()));
                dataList.add(Search3);

                //根据关键字搜索业主
                SearchEntity Search4 = new SearchEntity();
                List<Map<String,Object>>getSearchByOwnerList=SearchMapper.getSearchByOwnerList(searchList);
                Search4.setName(String.format("业主信息(%d)", getSearchByOwnerList.size()));
                Search4.setRouter("/sys-sell/contact");
                Search4.setList(getSearchByOwnerList.stream().map(myItem -> {
                    StringBuffer myItemStr = new StringBuffer();
                    String Status="";
                    //合同状态   0.履约中   1.已到期   2.未收录
                    if(!FieldCheckUtil.isMapFieldNotNull(myItem,new String[]{"OwDeId"})){
                        Status="未收录";
                    }else if(MapUtils.getInteger(myItem, "Status").equals(0)){
                        Status="履约中";
                    }else if(MapUtils.getInteger(myItem, "Status").equals(1)){
                        Status="已到期";
                    }
                    DateUtils.formatDateByMap(myItem);
                    myItemStr.append(HtmlUtil.BR)
                            .append("业主编号:").append(MapUtils.getString(myItem, "OwId"))
                            .append(HtmlUtil.BR)
                            .append("业主名称:").append(MapUtils.getString(myItem, "OwName"))
                            .append(HtmlUtil.BR)
                            .append("联系方式:").append(MapUtils.getString(myItem, "OwPhone"))
                            .append(HtmlUtil.BR)
                            .append("收录状态:").append(Status);

                    return HtmlUtil.stringToHTML(myItemStr.toString(), MapUtils.getString(myItem, "OwId"), Color.red);

                }).collect(Collectors.toList()));
                dataList.add(Search4);
            }
        }
        return JsonResult.success(dataList);
    }
}
